{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "aff78d5e",
   "metadata": {},
   "source": [
    "1. 泛型类与泛型接口"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "16facd89",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, Generics!\n",
      "42\n"
     ]
    }
   ],
   "source": [
    "class TestClass<T>{\n",
    "    public T data;\n",
    "}\n",
    "\n",
    "TestClass<String> stringInstance = new TestClass<String>();\n",
    "stringInstance.data = \"Hello, Generics!\";\n",
    "Console.WriteLine(stringInstance.data);\n",
    "\n",
    "TestClass<int> intInstance = new TestClass<int>();\n",
    "intInstance.data = 42;\n",
    "Console.WriteLine(intInstance.data);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c4067565",
   "metadata": {},
   "outputs": [],
   "source": [
    "interface ITestClass<T,k>{\n",
    "    T data1 { get; set; }\n",
    "    k data2 { get; set; }\n",
    "}\n",
    "\n",
    "class TestInterface:ITestClass<string, int>{\n",
    "    public string data1 { get; set; }\n",
    "    public int data2 { get; set; }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c85cf97e",
   "metadata": {},
   "source": [
    "2. 泛型方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1c044d3c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, Generics!\n",
      "42\n"
     ]
    }
   ],
   "source": [
    "class TestClassWithMethod{\n",
    "    public void TestFunc<T>(T data){\n",
    "        Console.WriteLine(data);\n",
    "    }\n",
    "\n",
    "    public void TestFunc2<T>(){\n",
    "        //判断T的类型,不同类型处理不同的逻辑\n",
    "        if(typeof(T) == typeof(string)){\n",
    "            Console.WriteLine(\"String type\");\n",
    "        }else if(typeof(T) == typeof(int)){\n",
    "            Console.WriteLine(\"Integer type\");\n",
    "        }\n",
    "    }\n",
    "\n",
    "    // 泛型方法返回默认值\n",
    "    public T TestFunc3<T>(){\n",
    "        return default(T);\n",
    "    }\n",
    "\n",
    "    //泛型参数可以传入多个\n",
    "}\n",
    "\n",
    "TestClassWithMethod testClass = new TestClassWithMethod();\n",
    "testClass.TestFunc(\"Hello, Generics!\");\n",
    "testClass.TestFunc<int>(42);"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cafebd3a",
   "metadata": {},
   "source": [
    "3. 泛型类中的泛型方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "586a28e8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, Generics!\n",
      "42\n"
     ]
    }
   ],
   "source": [
    "class TestClassWithMethod<T>{\n",
    "    //泛型类中的泛型方法\n",
    "    public void TestFunc<k>(k data){\n",
    "        Console.WriteLine(data);\n",
    "    }\n",
    "}\n",
    "\n",
    "TestClassWithMethod<string> testClass = new TestClassWithMethod<string>();\n",
    "testClass.TestFunc<String>(\"Hello, Generics!\");//<String>可以省略\n",
    "testClass.TestFunc<int>(42);"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fbc57c31",
   "metadata": {},
   "source": [
    "4. 泛型的作用\n",
    "    - 不同类型对象的相同的逻辑处理可以选择泛型\n",
    "    - 使用泛型可以一定程度上避免装箱拆箱"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "name": "polyglot-notebook"
  },
  "polyglot_notebook": {
   "kernelInfo": {
    "defaultKernelName": "csharp",
    "items": [
     {
      "aliases": [],
      "name": "csharp"
     }
    ]
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
